package com.ejoysoft.servlet;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Vector;

import javax.servlet.Servlet;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.Document;

import com.ejoysoft.common.Globa;
import com.ejoysoft.wlanyz.business.Task;


public class FetchTasksServlet extends HttpServlet implements Servlet
{
	private static final long serialVersionUID = 217251451801586160L;
	public FetchTasksServlet()
	{
		// TODO Auto-generated constructor stub
	}

	@Override
	protected void doGet(HttpServletRequest req,HttpServletResponse resp)throws ServletException, IOException {	// TODO Auto-generated method stub
		try {
			
		this.execute(req, resp);
		}catch(Exception e) {
			e.printStackTrace();
			throw new IOException(e.getMessage());
		}
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
	{
		// TODO Auto-generated method stub
		this.doGet(req, resp);
	}

	private void execute(HttpServletRequest req, HttpServletResponse resp) throws IOException
	{
		 Globa globa = new Globa(); 
		 StringBuffer sbReturn = new StringBuffer("<?xml version='1.0' encoding='utf-8'?> ");
			req.setCharacterEncoding("utf-8");		
			resp.setCharacterEncoding("utf-8");	
			try
			{
				
				Task task = new Task(globa,false);
					Vector<Task> vctAd = new Vector<Task>();
					vctAd = task.listLastState("  ", 0, 0);
					sbReturn.append("<info>");
					if (vctAd!=null && vctAd.size() > 0)
					{
//						sbReturn.append("<task>");
						for (int i = 0; i < vctAd.size(); i++)
						{
							System.out.println("12222"+vctAd.size());
							task = vctAd.get(i);
							if (task!=null)
							{
								sbReturn.append(returnTask(task));
							}
						}
//						sbReturn.append("</task>");
					}
					sbReturn.append("</info>");
				

			} catch (Exception e)
			{
				// TODO Auto-generated catch block
				e.printStackTrace();
			} finally
			{
				// 关闭数据库连接对象
				globa.closeCon();
				System.out.println(sbReturn.toString());
				resp.getWriter().println(sbReturn.toString());
			}
 
	}
	


	private StringBuffer returnTask(Task task)
	{
		StringBuffer sbReturnContent = new StringBuffer();
		if (task!=null) 
		{
			sbReturnContent.append("<task>");
			sbReturnContent.append("<strId>" + task.getStrId() + "</strId>");
			sbReturnContent.append("<strName>" + task.getStrName() + "</strName>");
			sbReturnContent.append("<strMac>" + task.getStrMac() + "</strMac>");
			sbReturnContent.append("<strTestContent>" + task.getStrTestContent() + "</strTestContent>");
			sbReturnContent.append("<strDesc>" + task.getStrDesc() + "</strDesc>");
			sbReturnContent.append("<dtCreateTime>" + task.getDtCreatTime() + "</dtCreateTime>");
			sbReturnContent.append("<strCreator>" + task.getStrCreator() + "</strCreator>");
			sbReturnContent.append("</task>");
		}
		return sbReturnContent;
	}
	

}

